package com.liu.user.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.liu.common.entity.PicturePath;
import com.liu.common.util.DateUtils;
import com.liu.common.util.EsayExcelUtils;
import com.liu.user.entity.vo.AdminGetUserInfoVO;
import com.liu.user.entity.vo.BlindBoxSaleInfoExportVO;
import com.liu.user.entity.vo.BlindBoxSaleInfoVO;
import com.liu.user.entity.vo.UserRoleVO;
import com.liu.user.mapper.AdminMapper;
import com.liu.user.service.AdminService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class AdminServiceImpl implements AdminService {

    @Resource
    private AdminMapper adminMapper;

    @Override
    public List<AdminGetUserInfoVO> getUserInfoList(String username) {
        return adminMapper.getUserInfoList(username);
    }

    @Override
    public PageInfo<UserRoleVO> getUserRoleList(String username,String pageNum,String pageSize) {
        PageInfo<UserRoleVO> userRoleSelectPageInfo = PageHelper.startPage(Integer.parseInt(pageNum), Integer.parseInt(pageSize)).doSelectPageInfo(() -> {
            adminMapper.getUserRoleList(username);
        });
        // 新建分页
        PageInfo<UserRoleVO> userRolePageInfo = new PageInfo<>();
        // 复制分页属性
        BeanUtils.copyProperties(userRoleSelectPageInfo, userRolePageInfo);
        // 对查询的list进行下一步操作
        List<UserRoleVO> userRoleVOS = new ArrayList<>(userRoleSelectPageInfo.getList());
        List<UserRoleVO> userRoleVOList = userRoleVOS.stream().sorted(Comparator.comparing(UserRoleVO::getRole)).collect(Collectors.toList());
        userRoleVOList.forEach(u->{
            if (u.getRole().equals("0")){
                u.setRole("管理员");
                u.setFunction("普通功能+商户功能+管理员功能");
            }
            if (u.getRole().equals("1")){
                u.setRole("商户用户");
                u.setFunction("普通功能+商户功能");
            }
            if (u.getRole().equals("2")){
                u.setRole("普通用户");
                u.setFunction("普通功能");
            }
        });
        userRolePageInfo.setList(userRoleVOList);
        return userRolePageInfo;
    }

    @Override
    public PageInfo<BlindBoxSaleInfoVO> getBlindBoxSaleInfo(String keyword, String pageNum, String pageSize) {
        String endTime = DateUtils.getDateFormat(new Date(), "yyyy-MM-dd HH:mm:ss");
        String startTime = DateUtils.getDateFormat(DateUtils.addDay(new Date(), -30), "yyyy-MM-dd HH:mm:ss");
        PageInfo<BlindBoxSaleInfoVO> blindBoxSaleInfoVOPageInfo = PageHelper.startPage(Integer.parseInt(pageNum), Integer.parseInt(pageSize)).doSelectPageInfo(() -> {
            adminMapper.getBlindBoxSaleInfo(keyword,startTime,endTime);
        });
        // 新建分页
        PageInfo<BlindBoxSaleInfoVO> blindBoxSaleInfoVOPageInfoResult = new PageInfo<>();
        // 复制分页属性
        BeanUtils.copyProperties(blindBoxSaleInfoVOPageInfo, blindBoxSaleInfoVOPageInfoResult);
        List<BlindBoxSaleInfoVO> blindBoxSaleInfoVOS = new ArrayList<>(blindBoxSaleInfoVOPageInfo.getList());
        List<BlindBoxSaleInfoVO> blindBoxSaleInfoVOSResult = blindBoxSaleInfoVOS.stream().sorted(Comparator.comparing(BlindBoxSaleInfoVO::getTotalProfit).reversed()).collect(Collectors.toList());
        blindBoxSaleInfoVOSResult.forEach(b->{
            if (b.getStatus().equals("0")){
                b.setStatus("在售");
            }
            if (b.getStatus().equals("1")){
                b.setStatus("已下架");
            }
        });
        blindBoxSaleInfoVOPageInfoResult.setList(blindBoxSaleInfoVOSResult);
        return blindBoxSaleInfoVOPageInfoResult;
    }

    @Override
    public void exportBlindBoxSaleInfo(String keyword, HttpServletResponse response) throws IOException {
        String endTime = DateUtils.getDateFormat(new Date(), "yyyy-MM-dd HH:mm:ss");
        String startTime = DateUtils.getDateFormat(DateUtils.addDay(new Date(), -30), "yyyy-MM-dd HH:mm:ss");
        List<BlindBoxSaleInfoVO> blindBoxSaleInfoVOList = adminMapper.getBlindBoxSaleInfo(keyword, startTime, endTime);
        List<BlindBoxSaleInfoExportVO> blindBoxSaleInfoExportVOS = new ArrayList<>();
        blindBoxSaleInfoVOList.forEach(b->{
            BlindBoxSaleInfoExportVO blindBoxSaleInfoExportVO = new BlindBoxSaleInfoExportVO();
            if (b.getStatus().equals("0")){
                b.setStatus("在售");
            }
            if (b.getStatus().equals("1")){
                b.setStatus("已下架");
            }
            blindBoxSaleInfoExportVO.setBlindBoxName(b.getBlindBoxName());
            blindBoxSaleInfoExportVO.setStatus(b.getStatus());
            blindBoxSaleInfoExportVO.setBlindBoxImage(PicturePath.BLIND_BOX_PICTURE_PATH+ "/" + b.getBlindBoxImage());
            blindBoxSaleInfoExportVO.setBlindBoxPrice(b.getBlindBoxPrice());
            blindBoxSaleInfoExportVO.setSaleQuantity(b.getSaleQuantity());
            blindBoxSaleInfoExportVO.setSaleAmount(b.getSaleAmount());
            blindBoxSaleInfoExportVO.setTotalCost(b.getTotalCost());
            blindBoxSaleInfoExportVO.setTotalProfit(b.getTotalProfit());
            blindBoxSaleInfoExportVOS.add(blindBoxSaleInfoExportVO);
        });
        try {
            EsayExcelUtils.writeExcel(response,blindBoxSaleInfoExportVOS,"近30天销售数据","近30天销售数据",BlindBoxSaleInfoExportVO.class);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

//    @Override
//    public UserDetailsVO getUserDetails(String username) {
//        return adminMapper.getUserDetails(username);
//    }

//    @Override
//    public List<AdminGetUserInfoVO> getUserInfoListByUsername(String username) {
//        List<AdminGetUserInfoVO> userInfoList = adminMapper.getUserInfoList();
//        List<AdminGetUserInfoVO> userInfoListByUsername = null;
//        userInfoList.forEach(u->{
//            if (u.getUsername().equals(username)){
//                userInfoListByUsername.add(u);
//            }
//        });
//        return userInfoListByUsername;
//    }
}
